package com.onpositive.gamemaker.swt;

import com.onpositive.gamemaker.core.CompositeSprite;
import com.onpositive.gamemaker.core.GameLevel;
import com.onpositive.gamemaker.core.GraphicDevice;
import com.onpositive.gamemaker.visualizer.HexagonMath;
import com.onpositive.gamemaker.visualizer.ISpriteProvider;
import com.onpositive.gamemaker.visualizer.swt.BasicLevelVisualizer;

public class SWTLevelVisualizer extends BasicLevelVisualizer
{
	protected int xLevelOffset;
	protected int yLevelOffset;

	public SWTLevelVisualizer(GraphicDevice gc, ISpriteProvider spriteProvider)
	{
		super(gc, spriteProvider);
		xLevelOffset = -cellDiameter / 4;
		yLevelOffset = -(int) Math.round(HexagonMath.TRIANGLE_MULTIPLIER * cellDiameter / 2);
	}
	
	@Override
	protected int getCellDiameter()
	{
		return VisualizerSettings.getCellDiameter();
	}
	
	@Override
	public void visualizeLevel(GameLevel level, int x, int y, int width, int height)
	{
		int startXcell = Math.min(x / (cellDiameter + cellDiameter / 2), level.getWidth() - 1);
		int startYcell = Math.min(y / yIncrement, level.getHeight() - 1);
		int endXcell = Math.min((x + width) / (cellDiameter + cellDiameter / 2), level.getWidth() - 1);
		int endYcell = Math.min(((y + height) / yIncrement) + 1, level.getHeight() - 1);
		
		if (startYcell > 0)
			startYcell -= 1;
		int currenty = (startYcell * yIncrement)/* - y*/;
		int xstart = 0;
		for (int i = startYcell; i < endYcell; i++)
		{
			xstart = cellDiameter / 4 * 3 * (i % 2);
			for (int j = startXcell; j <= endXcell; j++)
			{
				CompositeSprite sprites = spriteProvider.getSprites(j,i);
				sprites.paint(xstart + j * (cellDiameter + cellDiameter / 2) + xLevelOffset,currenty + yLevelOffset,cellDiameter,cellDiameter,gc);
			}
			currenty += yIncrement;
		}
	}

}
